$(function(){
	$('#fav').click(function(){
		addFavorite('金麟商城', $Util.getWebPath());
		return false;
	});
	setInterval("keepLive()",900000);
});

function keepLive(){
	var url = '/json?action=KEEP_LIVE_ACTION';
	$.post(url,{},function(data){},'json');
}

function addFavorite(title, url) {
	try {
		window.external.addFavorite(url, title);
	} catch (e) {
		try {
			window.sidebar.addPanel(title, url, '');
		} catch (e) {
			alert('收藏失败');
		}
	}
}

function getMemberSession(){
	var url = '/json?action=GET_MEMBER_SESSION_ACTION';
	$.post(url,{},function(data){
		if(data.success){
			var obj  = data.root.MEMBER_SESSION;
			if(obj){
				obj = obj.rs[0];
				$('#no_login').hide();
				$('#logined').show();
				$('#member_name').html(obj.MEMBER_ACCOUNT);
			}
		}
	},'json');
}
getMemberSession();

function toPage(to_url){
	var url = window.location.protocol + '//' + window.location.host + to_url;
	$Base.directPage(url);
}

function drawWinValidateCode() {
	var vc = new ValidateCode('/validateCode.jsp',60,18,4, 'WIN_MEMBER_REG');
	vc.applyTo('win_validateCode');
}

var winForm = {
	checkLoginName : function(){
		$('#win_login_message').hide();
		var loginName = $('#WIN_LOGIN_NAME');
		var loginNameVal = $.trim(loginName.val());
		if(loginNameVal.length == 0){
			var o = $('#win_login_message');
			o.html('请输入账号');
			o.show();
			return false;
		}
		return true;
	},
	
	checkPassword : function(){
		$('#win_login_message').hide();
		var pwd = $('#WIN_PASSWORD');
		var pwdVal = $.trim(pwd.val());
		if(pwdVal.length == 0){
			var o = $('#win_login_message');
			o.html('请输入密码');
			o.show();
			return false;
		}
		return true;
	},
	
	checkLoginForm : function(){
		return winForm.checkLoginName() && winForm.checkPassword();
	},
	
	checkUserName : function(){
		$('#win_reg_message').hide();
		var account = $('#WIN_MEMBER_ACCOUNT');
		var accountVal = $.trim(account.val());
		var o = $('#win_reg_message');
		if(accountVal.length == 0 || accountVal.length < 4  ||accountVal.length > 20){
			o.html('账号由4-20位中文、英文、数字、及"_"字符组成');
			o.show();
			return false;
		} else if(!/^[\u4E00-\u9FA5A-Za-z0-9]{4,20}$/g.test(accountVal)) {
			o.html('账号由4-20位中文、英文、数字、及"_"字符组成');
			o.show();
			return false;
		}
		
		var url = '/json?action=CHECK_MEMBER_NAME_ACTION&MEMBER_ACCOUNT='+ accountVal +'&random='+Math.random();
		$.post(encodeURI(url),{},function(data){
			if(!data.success){
				o.html('账号已存在');
				o.show();
			}
		},'json');
		return true;
	},
	
	checkRegPassword : function(){
		$('#win_reg_message').hide();
		var pwd = $('#WIN_REG_PASSWORD');
		var pwdVal = $.trim(pwd.val());
		var o = $('#win_reg_message');
		if(pwdVal.length == 0 || pwdVal.length < 8 || pwdVal.length > 20){
			o.html('密码长度在8-16位字符');
			o.show();
			return false;
		}
		return true;
	},
	
	checkConfirm : function(){
		$('#win_reg_message').hide();
		var rePwd = $('#WIN_RE_PASSWORD');
		var rePwdVal = $.trim(rePwd.val());
		
		var pwd = $('#WIN_REG_PASSWORD');
		var pwdVal = $.trim(pwd.val());
		var o = $('#win_reg_message');
		if(rePwdVal.length == 0 || rePwdVal != pwdVal){
			o.html('两次密码不一致');
			o.show();
			return false;
		}
		return true;
	},
	
	checkCode : function(){
		$('#win_reg_message').hide();
		var vcode = $('#WIN_VALIDATE_CODE');
		var vcodeVal = $.trim(vcode.val());
		var o = $('#win_reg_message');
		if(vcodeVal.length == 0){
			o.html('请输入验证码');
			o.show();
			return false;
		}
		return true;
	},
	
	checkAgree : function(){
		$('#win_reg_message').hide();
		var flag = $("#win_agree").attr("checked");
		if(!flag){
			if(confirm("如果不同意金麟条款，则不能进行注册，点击确定表示同意，点击取消将退出注册")){
				return true;
			}
			else{
				location.href = "../index.html";
				return false;
			}
		}
		return true;
	},
	
	checkRegForm : function(){
		return winForm.checkAgree() && winForm.checkCode() && winForm.checkUserName() && winForm.checkRegPassword() && winForm.checkConfirm();
	},
	
	clearTip : function(){
		$('#win_login_message').hide();
		$('#win_reg_message').hide();
	}
}

function exit(){
	var url = '/json?action=MEMBER_LOGOUT_ACTION';
	$.post(url,{},function(data){
		if(data.success){
			location.href="/index.html";
		}
	},'json');
}
